/**
 * Created by 余保林 on 2017/8/8.
 */
import {Message} from 'iview'
import axios from 'axios'

function ajax ({method, url, success, params = {}, data = {}, fail = null}) {
  axios({
    url,
    method,
    params,
    data,
    timeout: 2000
  }).then((res) => {
    let resData = res.data
    if (resData.code === 0) {
      success(resData.data)
      return
    }
    Message.warning(resData.msg)
    if (fail) {
      fail(resData.msg)
    }
  }).catch((err) => {
    Message.error(err.message)
    if (fail) {
      fail(err.message)
    }
  })
}

export default {
  get: ({url, params = {}, success, fail = null}) => {
    ajax({method: 'get', url, success, params, fail})
  },
  post: ({url, data = {}, success, fail = null}) => {
    ajax({method: 'post', url, success, data, fail})
  },
  put: ({url, data = {}, success, fail = null}) => {
    ajax({method: 'put', url, success, data, fail})
  },
  del: ({url, params, data = {}, success, fail = null}) => {
    ajax({method: 'delete', url, params, success, data, fail})
  }
}
